#include "addon.h"
#include <windows.h>

CAddon::CAddon(){
	addon_element=0;
	entity=0;
}

void CAddon::parse(TiXmlElement *element){
	addon_element=element;
	authors=makeList("authors","author");
	compatibility=makeList("compatibility","build");
	name=getText("name");
	version=getText("version");
	license=getText("license");
	category=getText("category");
	desc=getText("description");
	link=getText("upload","link");
	checksum=getText("upload","checksum");
	required=getDependency("required");
	optional=getDependency("optional");
}

CAddon::CAddon(TiXmlElement *element){
	parse(element);
}

CAddon::~CAddon(){
	if (addon_element)
		delete addon_element;
	if (entity)
		delete entity;
}


char * CAddon::getText(char *tag){
	return (char *)(addon_element->FirstChildElement(tag))->GetText();
}

char * CAddon::getText(char *tag, char *tag1){
	TiXmlElement *tmp=addon_element->FirstChildElement(tag);
	return (char *)(tmp->FirstChildElement(tag1))->GetText();
}

std::vector<CString> CAddon::makeList (char * tag, char *tags){
	std::vector<CString> list;
	TiXmlElement *tmp=addon_element->FirstChildElement(tag);
	entity=tmp->FirstChildElement(tags);
	while(entity!=NULL){
		list.push_back(entity->GetText());
		entity=entity->NextSiblingElement(tags);
	}
	return list;
}

std::vector<dependency> CAddon::getDependency(char *tag){
	std::vector<dependency> depend;
	TiXmlElement *tmp=addon_element->FirstChildElement(tag);
	entity=tmp->FirstChildElement("addon");
	while(entity!=NULL){
		struct dependency dep;
		dep.name=entity->Attribute("name");
		dep.version=entity->Attribute("version");
		depend.push_back(dep);
		entity=entity->NextSiblingElement("addon");
	}
	return depend;
}
